package com.dlyk.web;

import com.dlyk.model.Tran;
import com.dlyk.query.TranQuery;
import com.dlyk.result.R;
import com.dlyk.service.TranService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 交易
 */
@RestController
public class TranController {

    @Resource
    private TranService tranService;

    /**
     * 创建交易（添加）
     */
    @PostMapping("/api/tran/add")
    public R addTran(TranQuery tranQuery) {

        int add = tranService.addTran(tranQuery);

        return add >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 查询交易列表（分页查询）
     */
    @PreAuthorize(value = "hasAuthority('tran:list')")
    @GetMapping("/api/tran/list")
    public R tranList(Integer current) {

        PageInfo<Tran> tranPageInfo = tranService.getTranListByPage(current);

        return R.OK(tranPageInfo);
    }

    /**
     * 根据id查询交易详情
     */
    @PreAuthorize(value = "hasAuthority('tran:detail')")
    @GetMapping("/api/tran/detail/{id}")
    public R tranDetail(@PathVariable Integer id) {

        Tran tran = tranService.getTranDetailById(id);

        return R.OK(tran);
    }

    /**
     * 编辑交易
     */
    @PreAuthorize(value = "hasAuthority('tran:edit')")
    @PutMapping("/api/tran/edit")
    public R editTran(TranQuery tranQuery) {

        int edit = tranService.editTran(tranQuery);

        return edit >= 1 ? R.OK() : R.FAIL();
    }

}

